﻿using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class RoomBll : IRoomBll
    {
        public RoomBll(IYkDal<Room> ykDal)
        {
            YkDal = ykDal;
        }
        public IYkDal<Room> YkDal { get; }
        /// <summary>
        /// 居民信息录入
        /// </summary>
        /// <param name="room">表名</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> Tian(Room room)
        {
        return YkDal.Add(room);
        }
        /// <summary>
        ///  显示居民信息
        /// </summary>
        /// <param name="name">居民姓名</param>
        /// <param name="types">居民类型</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">页容量</param>
        /// <returns></returns>
        public dynamic Xian(string? name, string? types, int PageIndex, int PageSize)
        {
                var list = YkDal.Show();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    list = list.Where(x => x.Name.Contains(name));
                }
                if (!string.IsNullOrWhiteSpace(types))
                {
                    list = list.Where(x => x.Type == types);
                }
                var con = list.Count();
                var lis = list.OrderBy(x => x.RoomId).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
                return new
                {
                    con,
                    lis
                };
        }
        /// <summary>
        /// 批量删除功能（逻辑删除）
        /// </summary>
        /// <param name="sid">sid标识符</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> PlLjDelete(string sid)
        {
            var lists = YkDal.Show();
            List<int> ints=sid.Split(",").Select(x=>Convert.ToInt32(x)).ToList();
            lists = lists.Where(x => ints.Contains(x.RoomId));
            foreach (var item in lists)
            {
                item.Ljsc = false;
            }
            return YkDal.PlUpdate(lists.ToList());
        }
        /// <summary>
        /// 批量审核功能（修改状态为已审核）
        /// </summary>
        /// <param name="sid">sid标识符</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> PlShUpdate(string sid)
        {
            var lists = YkDal.Show();
            List<int> ints = sid.Split(",").Select(x => Convert.ToInt32(x)).ToList();
            lists = lists.Where(x => ints.Contains(x.RoomId));
            foreach (var item in lists)
            {
                item.ShState=true;
            }
            return YkDal.PlUpdate(lists.ToList());
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="room">表名</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> Xiu(Room room)
        {
            return YkDal.Update(room);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id">id标识符</param>
        /// <returns></returns>
        public Room Fan(int id)
        {
            return YkDal.Show().FirstOrDefault(x=>x.RoomId==id);
        }
    }
}